<?php
namespace App\DBs;

use Illuminate\Support\Facades\DB;

class VideoDB extends BaseDB
{
    /**
     * 视频
     */

    public $tFarm = 'gg_farm';
    public $tVideo = 'gg_video';
    public $tOwner = 'gg_owner';

    public function __construct()
    {
    }

    //农场产品列表
    public function getFarmIndex($cate,$page,$limit)
    {
        $page = $page ? $page-1 : $page;
        $page = $page * $limit;
        $res = DB::table($this->tFarm)->where('del',0);
        if ($cate) { $res = $res->where('cate',$cate); }
        $total = $res->count();
        $res = $res->orderBy('id','asc')->skip($page)->take($limit)->get();
        if (!count($res)) {
            return array('code'=>'-1','msg'=>'no data');
        }
        $res = $this->jsonToArr($res);
        $items = array();
        foreach ($res as $re) {
            $proNum = '0 / '.$re['num'].'颗，还有999颗';
            if ($re['endday']) {
                $endDay = strtotime($re['endday']);
                $day = ceil((time() - $endDay)/3600/24);
                $endDay = date('Y年m月d日',$endDay).'，还剩'.$day.'天';
            } else {
                $endDay = '';
            }
            $videoOnes = DB::table($this->tVideo)->where('farm',$re['id'])
                ->orderBy('id','asc')->skip(0)->take(1)->get();
            $videoOne = count($videoOnes) ? $videoOnes[0] : '';
            $proImg = $videoOne ? $videoOne->outcover : '/imgs/pro0.png';
            $items[] = [
                'proId'=>(string)$re['id'], 'proName'=>$re['title'],
                'proDesc'=>$re['detail'], 'proFarm'=>$re['farm'],
                'proNum'=>$proNum, 'endDay'=>$endDay,
                //'proImg'=>'/imgs/pro0.png',
                'proImg'=>$proImg,
            ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items,'total'=>$total);
    }

    //视频列表
    public function getVideoIndex($cate,$farm,$page,$limit)
    {
        $page = $page ? $page-1 : $page;
        $page = $page * $limit;
        $res = DB::table($this->tVideo)->where('del',0);
        if ($cate) { $res = $res->where('cate',$cate); }
        if ($farm) { $res = $res->where('farm',$farm); }
        $total = $res->count();
        $res = $res->orderBy('id','asc')->skip($page)->take($limit)->get();
        if (!count($res)) {
            return array('code'=>'-1','msg'=>'no data');
        }
        $res = $this->jsonToArr($res);
        $items = array();
        foreach ($res as $re) {
            $farms = DB::table($this->tFarm)->where('id',$re['farm'])
                ->orderBy('id','asc')->skip(0)->take(1)->get();
            $farm = count($farms) ? $farms[0] : '';
            $farmName = $farm ? $farm->farm : '';
            $cates = ['','苹果','橙子'];
            $cateName = array_key_exists($re['cate'],$cates) ? $cates[$re['cate']] : '';
            $items[] = [
                'vid'=>(string)$re['id'], 'vtitle'=>$re['title'],
                'vcover'=>$re['outcover'], 'vlink'=>$re['outlink'],
                'farmName'=>$farmName, 'cateName'=>$cateName,
            ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items,'total'=>$total);
    }

    //领取
    public function getOwnerIndex($uid)
    {
        $res = DB::table($this->tOwner)->where('del',0)->where('isout',0)
            ->where('uid',$uid)->orderBy('id','desc')->get();
        if (!count($res)) {
            return array('code'=>'-1','msg'=>'no data');
        }
        $res = $this->jsonToArr($res);
        $items = array();
        foreach ($res as $re) {
            $farms = DB::table($this->tFarm)->where('id',$re['farm'])
                ->orderBy('id','asc')->skip(0)->take(1)->get();
            $farm = count($farms) ? $farms[0] : '';
            $farmId = $farm ? $farm->id : 0;
            $farmName = $farm ? $farm->title : '';
            $total = $farm ? $farm->num : 0;
            if ($farm->endday) {
                $endDay = strtotime($farm->endday);
                $day = ceil((time() - $endDay)/3600/24);
                $endDay = date('Y年m月d日',$endDay).'，还剩'.$day.'天';
            } else {
                $endDay = '';
            }
            $videos = DB::table($this->tVideo)->where('farm',$re['farm'])
                ->orderBy('id','asc')->skip(0)->take(1)->get();
            $video = count($videos) ? $videos[0] : '';
            $farmImg = $video ? $video->outcover : '/imgs/pro0.png';
            $items[] = [
                'id'=>$re['id'], 'uid'=>$re['uid'], 'farmId'=>$farmId,
                'farmName'=>$farmName, 'farmImg'=>$farmImg,
                'farmTotal'=>$total, 'farmNum'=>0, 'farmEndDay'=>$endDay,
            ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items);
    }
}